package com.jadcon.demos;

import java.io.File;
import java.io.IOException;

public class DirectoryLister {

	public static void main(String args[]) {
		if (args.length < 1) {
			printUsage();
		}
		File file = new File(args[0]);
		if (file.exists() && file.isDirectory()) {
			try {
				listFiles(file);
			} catch (IOException e) {
				System.err.println("IOException trying to list files");
				e.printStackTrace();
			}
		} else {
			System.out.println(args[0] + " does not exist or is not a directory");
		}
	}
	
	private static void listFiles(File file) throws IOException {
		System.out.println(file.getCanonicalPath());
		for (File child : file.listFiles()) {
			if (child.isDirectory()) {
				listFiles(child);
			} else {
				System.out.println(child.getCanonicalPath());
			}
		}
	}
	
	private static void printUsage() {
		System.out.println("Usage: DirectoryLister {directory name}");
		// anything other than zero generally indicates a 
		// program exited with an error
		System.exit(1);
	}
}
